package com.java.mybatis.controller;

import com.java.mybatis.entity.Department;
import com.java.mybatis.service.DepartmentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Department)表控制层
 *
 * @author makejava
 * @since 2021-01-15 17:10:28
 */
@Controller
@RequestMapping("/department/")
public class DepartmentController {
    /**
     * 服务对象
     */
    @Resource
    private DepartmentService departmentService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Department selectOne(Integer id) {
        return this.departmentService.queryById(id);
    }
    /**
     * 查询所有部门信息的方法
     * @return 序列对象
     * */
    @GetMapping("queryAllDept")
    public String queryAllDept(Model model){
        //调用查询所有部门信息的方法
        List<Department> departments = departmentService.queryAllDept();
        //将查询到的信息存入model中
        model.addAttribute("dept",departments);
        //得到总条数的方法
        model.addAttribute("ids",departments.size());
        //返回页面
        return "System/question-list";
    }
    /**
     * 根据部门编号或者部门名称来查询部门信息
     * @param departmentName 部门名称
     * @return 对象序列
     * */
    @GetMapping("selectByLike")
    public String selectByLike(Model model,String departmentName){
        //测试
        //System.out.println("获取部门编号测试:"+departmentId);
        System.out.println("获取部门名称测试:"+departmentName);
        //调用查询部门编号或者部门名称的方法
        List<Department> departments = departmentService.selectByLike( departmentName);
        //查返回的值存入model中
        model.addAttribute("dept",departments); //得到总条数的方法
        model.addAttribute("ids",departments.size());
        //返回对应的页面
        return "System/question-list";
    }
    /**
     *新增数据的方法
     * @param department 实例对象
     * @return 受影响行数
     * */
    @GetMapping("insertDept")
    public String insertDept(Model model,Department department){
        //调用新增数据的方法
        Department i = departmentService.insert(department);
        System.out.println("测试是否添加成功:"+i);
        if(i!=null && i.equals(" ")){
            //将返回的数据存入Model中
            model.addAttribute("depts",i);
            //返回相关页面
            return "System/question-list";
        }else{
            System.out.println("添加失败");
        }
        return "System/question-add";
    }
    /**
     *修改部门信息的方法
     * @param department 实例对象
     * @return 受影响行数
     * */
    @GetMapping("updateDept")
    public String updateDept(Model model,Department department){
        System.out.println("测试是否能获取到数据:"+department);
        //调用修改部门信息的方法
        int u = departmentService.update(department);
        if(u>0){
            //将获取到的数据存入model中
            model.addAttribute("updateDept",u);
            //返回对应的页面
            return "System/question-list";
        }else{
            System.out.println("修改数据失败");
        }
       return "System/question-edit";
    }
    /**
     *根据ID删除部门信息的方法
     * @param departmentId 部门编号
     * @return 受影响行数
     * */
    @GetMapping("deleteById")
    public String deleteById(Model model,Integer departmentId){
        //调用删除部门信息的方法
        boolean b = departmentService.deleteById(departmentId);
        System.out.println(b);
        if(b){
            //将返回的数据存入Model中
            model.addAttribute("departmentId",b);
            //跳转至对应的页面
            return "System/question-list";
        }else{
            System.out.println("删除数据失败");
        }
        return "";
    }
    /**
     * 启用的方法
     * @param id
     * @return
     */
    @RequestMapping("upDepartmentUp")
    public String upDepartmentUp(Integer id){
        System.out.println("部门编号:"+id);
        try {
            //调用启用的方法
            departmentService.upDepartmentCodeUp(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/department/queryAllDept";
    }

    /**
     * 禁用的方法
     * @param id
     * @return
     */
    @RequestMapping("upDepartmentDown")
    public String upDepartmentDown(Integer id){
        System.out.println("部门编号111:"+id);
        try {
            //调用禁用的方法
            departmentService.upDepartmentCodeDown(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/department/queryAllDept";
    }
    /**
     * 批量删除的方法
     * @return
     */
    @RequestMapping("upDepartmentAll")
    public String upDepartmentAll(String str) throws Exception {
        String[] split = str.split(",");
        System.out.println("获取到的所有部门编号:"+str);
        for (String s : split) {
            System.out.println("获取到的所有部门编号:"+s);
            departmentService.upDepartmentCodeDown(Integer.parseInt(s));
        }
        return "redirect:/department/queryAllDept";
    }

}